package cn.foolishbird.crow.core;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author foolishbird
 */
public final class CashUtils {

    private CashUtils() {

    }

    /**
     * convert yuan to fen
     *
     * @param yuan yuan
     * @return fen
     */
    public static Long toRent(Number yuan) {
        if (Objects.isNull(yuan)) {
            return 0L;
        }
        return toRent(new BigDecimal(yuan.toString()));
    }

    /**
     * convert yuan to fen
     *
     * @param yuan yuan
     * @return fen
     */
    public static Long toRent(BigDecimal yuan) {
        if (Objects.isNull(yuan)) {
            return 0L;
        }
        return yuan.multiply(BigDecimal.valueOf(100)).longValue();
    }

    /**
     * convert fen to yuan
     *
     * @param rent rent
     * @return yuan
     */
    public static BigDecimal toYuan(Long rent) {
        if (Objects.isNull(rent)) {
            return BigDecimal.ZERO;
        }
        return BigDecimal.valueOf(rent)
                .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
    }

    /**
     * convert fen to yuan
     *
     * @param rent rent
     * @return yuan
     */
    public static String toYuanStr(Long rent) {
        return toYuan(rent).toPlainString();
    }

    /**
     * @param f yuan
     * @return yuan
     */
    public static String toYuanStr(Double f) {
        if (Objects.isNull(f)) {
            return "0.00";
        }
        return BigDecimal.valueOf(f)
                .setScale(2, RoundingMode.HALF_UP).toPlainString();
    }

}
